package com.example.demo6.ui.center;

import androidx.lifecycle.ViewModelProvider;

import android.os.AsyncTask;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;

import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.demo6.R;
import com.example.demo6.SQLite.MyDatabase;
import com.example.demo6.SQLite.User;

public class centerFragment extends Fragment {
    String username;
    TextView showusername,showphone,showregion;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        // 如果没有有效的username，显示一个临时的View或消息，而不是导航
        if (getArguments() == null || TextUtils.isEmpty(getArguments().getString("username"))) {
//            // 例如，创建一个简单的TextView显示登录提示
//            TextView loginPrompt = new TextView(getContext());
//            loginPrompt.setText("请先登录！");
//            loginPrompt.setGravity(Gravity.CENTER);
//            loginPrompt.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
//            return loginPrompt;

            View customView = inflater.inflate(R.layout.login_prompt, container, false);
            TextView confirmButton = customView.findViewById(R.id.unlogin);
            // 设置确认按钮的点击监听器
            confirmButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // 用户点击确认按钮时，导航到登录界面
                    NavController navController = Navigation.findNavController(getActivity(), R.id.nav_host_fragment_activity_main); // 使用正确的nav_host_fragment ID
                    navController.navigate(R.id.loginFragment);
                }
            });
            return customView;
        }

        View view = inflater.inflate(R.layout.fragment_center, container, false);

        // 初始化已有的视图和逻辑...
        username = getArguments().getString("username");
        showusername = view.findViewById(R.id.showusername);
        showphone = view.findViewById(R.id.showphone);
        showregion = view.findViewById(R.id.showregion);
        Button buttonMyInformation = view.findViewById(R.id.editmyinformation);
        buttonMyInformation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                navigateTo(R.id.edit_personal_informationFragment);
            }
        });

        // 添加对新按钮的处理
        Button buttonMyLose = view.findViewById(R.id.mylosebutton);
        buttonMyLose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                navigateTo(R.id.myloseFragment);
            }
        });

        Button buttonMyPickup = view.findViewById(R.id.mypickupbutton);
        buttonMyPickup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                navigateTo(R.id.mypickupFragment);
            }
        });

        new UserTask(username).execute();
        return view;
    }

    // 为了简化导航操作，可以创建一个辅助方法
    private void navigateTo(int destinationId) {
        NavController navController = Navigation.findNavController(requireView());
        Bundle bundle=new Bundle();
        bundle.putString("username", username);//传参
        navController.navigate(destinationId,bundle);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

    }

    private class UserTask extends AsyncTask<Void, Void, User> {
        private String username;

        public UserTask(String username) {
            this.username = username;
        }

        @Override
        protected User doInBackground(Void... voids) {
            return MyDatabase.getInstance(requireContext()).userDao().getUserByUsername(username);
        }

        @Override
        protected void onPostExecute(User user) {
            showphone.setText(String.valueOf(user.getPhone()));
            showregion.setText(user.getRegion());
            showusername.setText(user.getUsername());

        }
    }
}